//
//  ShowMenuView.m
//  EMMApp
//
//  Created by Mac on 16/5/10.
//  Copyright © 2016年 YiXun. All rights reserved.
//

#import "ShowMenuView.h"

@interface ShowMenuView ()<UIPickerViewDelegate,UIPickerViewDataSource,UIPickerViewAccessibilityDelegate>
{
    NSString *_showString;
    NSString *_number;
}
@property (nonatomic, copy) NSArray *numArr;
@property (nonatomic, copy) NSArray *dataArr;
@property (nonatomic, strong) NSString *titleString;
@property (nonatomic, strong) NSString *nameString;
@end

@implementation ShowMenuView

- (id)initWithFrame:(CGRect)frame title:(NSString *)title titleNmae:(NSString *)titleNmae numLits:(NSArray *)numList items:(NSArray *)items {
    if (self == [super initWithFrame:frame]) {
        self.titleString = title;
        self.dataArr = numList;
        self.numArr = items;
        self.nameString = titleNmae;
        [self showUI];
    }
    return self;
}

#pragma 创建视图初始化
- (void)showUI {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH, EMM_HEIGHT)];
    view.backgroundColor = [UIColor clearColor];
    view.userInteractionEnabled = YES;
    [self addSubview:view];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
    [view addGestureRecognizer:tap];
    
    UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, EMM_HEIGHT-275, EMM_WIDTH, 275)];
    subView.backgroundColor = [UIColor whiteColor];
    subView.userInteractionEnabled = YES;
    [self addSubview:subView];
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH, 54)];
    title.textAlignment = NSTextAlignmentCenter;
    title.text = self.nameString;
    title.font = [UIFont systemFontOfSize:16];
    [subView addSubview:title];
    
    UIView *hxView = [[UIView alloc] initWithFrame:CGRectMake(0, 54, EMM_WIDTH, 1)];
    hxView.backgroundColor = [UIColor colorFromHexCode:@"#CCCCCC"];
    [subView addSubview:hxView];
    
    UIImageView *imaV = [[UIImageView alloc] initWithFrame:CGRectMake(15, 17.5, 20, 20)];
    imaV.image = [UIImage imageNamed:@"close"];
    [subView addSubview:imaV];
    
    UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom];
    cancel.frame = CGRectMake(0, 0, 60, 55);
//    [cancel setImage:[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    
    [cancel addTarget:self action:@selector(cancelClick:) forControlEvents:UIControlEventTouchUpInside];
    [subView addSubview:cancel];
    
    UILabel *l1 = [[UILabel alloc] initWithFrame:CGRectMake(0, (subView.frame.size.height-20)/2-6, subView.frame.size.width/2, 0.5)];
    l1.backgroundColor = [UIColor lightGrayColor];
//    [subView addSubview:l1];
    UILabel *titleName = [[UILabel alloc] initWithFrame:CGRectMake(10, (subView.frame.size.height-20)/2, subView.frame.size.width/2, 20)];
    titleName.text = self.nameString;
    titleName.font = [UIFont systemFontOfSize:16];
//    [subView addSubview:titleName];
    UILabel *l2 = [[UILabel alloc] initWithFrame:CGRectMake(0, (subView.frame.size.height-20)/2+20.5, subView.frame.size.width/2, 0.5)];
    l2.backgroundColor = [UIColor lightGrayColor];
//    [subView addSubview:l2];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(hxView.frame.size.width - 65, 7.5, 60, 40);
//    btn.backgroundColor = [UIColor colorFromHexCode:@"#e97300"];
    [btn setTitleColor:[UIColor colorFromHexCode:@"#1A1A1A"] forState:UIControlStateNormal];
    [btn setTitle:@"保存" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchUpInside];
//    btn.layer.masksToBounds = YES;
//    btn.layer.cornerRadius = 5;
    btn.titleLabel.font = [UIFont systemFontOfSize:16];
    [subView addSubview:btn];
    
    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 55, EMM_WIDTH, subView.frame.size.height-55)];
    pickerView.backgroundColor = [UIColor whiteColor];
    pickerView.delegate = self;
    pickerView.dataSource = self;
    pickerView.showsSelectionIndicator = YES;    //这个最好写 你不写来试下哇
    [pickerView selectRow:0 inComponent:0 animated:NO];
    if (_numArr.count > 0) {
        _showString=[_numArr objectAtIndex:0];
        _number = @"1";
    }
    [subView addSubview:pickerView];
}

#pragma 点击阴影视图消失
- (void)tapClick:(UITapGestureRecognizer *)tap {
    if (_touchBlock) {
        _touchBlock(nil,nil);
    }
}

#pragma 取消按钮
- (void)cancelClick:(UIButton *)btn {
    if (_touchBlock) {
        _touchBlock(nil,nil);
    }
}

#pragma 保存按钮
- (void)btnclick:(UIButton *)btn {
    if ([NSString isStringEmpty2:_showString] == NO) {
        if (_touchBlock) {
            _touchBlock(_showString,_number);
        }
    }else {
        if (_touchBlock) {
            _touchBlock(nil,nil);
        }
    }
}

#pragma pickerView代理实现
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return _numArr.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [_numArr objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    _showString = [_numArr objectAtIndex:row];
    _number = [NSString stringWithFormat:@"%ld",row +1];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];
        pickerLabel.textAlignment = NSTextAlignmentCenter;
    }
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 40.0;
}

@end
